import requests
import mysql.connector

def pa(l):
    #学校名称
    schoolName = "华中科技大学"

    #爬取链接
    url = "https://api.haitou.cc/live/univ-yxj?univ=hust-zph&action="+l

    #请求头
    headers = {

    }
    #爬取数据并解析
    html = requests.get(url=url, headers=headers)

    # 解析JSON字符串,获取数据列表
    dataList = html.json().get("data")

    for data in dataList:
        # 获取JSON字符串中指定的键值对
        print(data.get("name")+"|"+data.get("start_time"))
        mysql_conn(data.get("name"),schoolName,data.get("start_time"))


#数据库连接
def mysql_conn(job_title,school_name,create_time):
    mydb = mysql.connector.connect(
        host="127.0.0.1",
        user="root",
        passwd="",
        database="hschool_job"
    )
    mycursor = mydb.cursor()

    #插入
    sql = "INSERT INTO h_job (job_title,school_name,create_time) VALUES (%s,%s,%s)"
    val = (job_title,school_name,create_time)
    mycursor.execute(sql, val)
    mydb.commit()

if __name__ == '__main__':
    for i in range(1,14):
        pa(str(i))
